package com.logistics.distribution;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.logistics.database.Distributiondatabase;

public class Storeroomconfigservlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Storeroomconfigservlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		response.setContentType("text/xml;charset=UTF-8");
	    response.setHeader("Cache-Control", "no-cache");
		if(action.equals("refresh"))
		{
			String storeroom_name = URLDecoder.decode(request.getParameter("storeroom_name"), "utf-8");
			if(storeroom_name==null)
				storeroom_name="";
			String storeroom_address = URLDecoder.decode(request.getParameter("storeroom_address"), "utf-8");
			if(storeroom_address==null)
				storeroom_address="";
			String storeroom_manager = URLDecoder.decode(request.getParameter("storeroom_manager"), "utf-8");
			if(storeroom_manager==null)
				storeroom_manager="";
			String storeroom_level = URLDecoder.decode(request.getParameter("storeroom_level"), "utf-8");
			if(storeroom_level==null)
				storeroom_level="";
			//System.out.println("storeroom_name:"+storeroom_name);
			//System.out.println("storeroom_address:"+storeroom_address);
			//System.out.println("storeroom_manager:"+storeroom_manager);
			//System.out.println("storeroom_level:"+storeroom_level);
			ArrayList<Storeroom> storerooms = new ArrayList<Storeroom>();
			Distributiondatabase distributiondatabase= new Distributiondatabase();
			ResultSet resultSet = distributiondatabase.getStoreroom();
			try {
				while(resultSet.next())
				{
					int id = resultSet.getInt("storeroom_id");
					String name = resultSet.getString("storeroom_name");
					String address = resultSet.getString("address");
					String manager = resultSet.getString("storeroom_manager");
					String level = resultSet.getString("store_level");
					storerooms.add(new Storeroom(id, name, address, manager, level));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//System.out.println("!"+storerooms.size());
			if(!storeroom_name.equals(""))
			{
				for(int i = storerooms.size()-1;i>=0;--i)
				{
					if(!storerooms.get(i).getStoreroom_name().equals(storeroom_name))
					{
						storerooms.remove(i);
					}
				}
			}
			if(!storeroom_address.equals(""))
			{
				for(int i = storerooms.size()-1;i>=0;--i)
				{
					if(!storerooms.get(i).getStoreroom_address().equals(storeroom_address))
					{
						storerooms.remove(i);
					}
				}
			}
			if(!storeroom_manager.equals(""))
			{
				for(int i = storerooms.size()-1;i>=0;--i)
				{
					if(!storerooms.get(i).getStoreroom_manager().equals(storeroom_manager))
					{
						storerooms.remove(i);
					}
				}
			}
			if(!storeroom_level.equals(""))
			{
				for(int i = storerooms.size()-1;i>=0;--i)
				{
					if(!storerooms.get(i).getStoreroom_level().equals(storeroom_level))
					{
						storerooms.remove(i);
					}
				}
			}
			String xml_start ="<storerooms>";
			String xml_end = "</storerooms>";
			String xml = ""; 
			for(int i = 0;i<storerooms.size();++i)
			{
				xml+="<storeroom><name>"+storerooms.get(i).getStoreroom_name()+"</name><address>"+storerooms.get(i).getStoreroom_address()+"</address><manager>"+storerooms.get(i).getStoreroom_manager()+"</manager><level>"+storerooms.get(i).getStoreroom_level()+"</level><id>"+storerooms.get(i).getStoreroom_id()+"</id></storeroom>";
			}
			String xml_last = xml_start+xml+xml_end;
			//System.out.println(xml_last);
			response.getWriter().write(xml_last);
			response.getWriter().close();
		}
		else if(action.equals("mod"))
		{
			String storeroom_name = URLDecoder.decode(request.getParameter("storeroom_name"), "utf-8");
			if(storeroom_name==null)
				storeroom_name="";
			String storeroom_address = URLDecoder.decode(request.getParameter("storeroom_address"), "utf-8");
			if(storeroom_address==null)
				storeroom_address="";
			String storeroom_manager = URLDecoder.decode(request.getParameter("storeroom_manager"), "utf-8");
			if(storeroom_manager==null)
				storeroom_manager="";
			String storeroom_level = URLDecoder.decode(request.getParameter("storeroom_level"), "utf-8");
			if(storeroom_level==null)
				storeroom_level="";
			String storeroom_id = request.getParameter("storeroom_id");
			Distributiondatabase distributiondatabase = new Distributiondatabase();
			distributiondatabase.updateStoreroom(storeroom_id, storeroom_name, storeroom_address, storeroom_manager, storeroom_level);
			response.getWriter().write("");
			response.getWriter().close();
		}
		else if(action.equals("delete"))
		{
			String storeroom_id = request.getParameter("storeroom_id");
			Distributiondatabase distributiondatabase = new Distributiondatabase();
			distributiondatabase.deleteStoreroom(Integer.valueOf(storeroom_id));
			response.getWriter().write("");
			response.getWriter().close();
		}
		else if(action.equals("add"))
		{
			String storeroom_name = URLDecoder.decode(request.getParameter("storeroom_name"), "utf-8");
			if(storeroom_name==null)
				storeroom_name="";
			String storeroom_address = URLDecoder.decode(request.getParameter("storeroom_address"), "utf-8");
			if(storeroom_address==null)
				storeroom_address="";
			String storeroom_manager = URLDecoder.decode(request.getParameter("storeroom_manager"), "utf-8");
			if(storeroom_manager==null)
				storeroom_manager="";
			String storeroom_level = URLDecoder.decode(request.getParameter("storeroom_level"), "utf-8");
			if(storeroom_level==null)
				storeroom_level="";
			Distributiondatabase distributiondatabase = new Distributiondatabase();
			distributiondatabase.addStoreroom(storeroom_name, storeroom_address, storeroom_manager, storeroom_level);
			response.getWriter().write("");
			response.getWriter().close();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
